package com.erp.erp_ui.Cost.Service;

import com.erp.erp_entitys.Cost.entity.CostApportionmentEntity;
import com.erp.erp_entitys.Cost.entity.CostDetailEntity;
import com.erp.erp_entitys.Cost.req.InsertPlanInStoreProductInfoReq;
import com.erp.erp_entitys.warehouse.entity.PlanInstoreProductEntity;
import com.erp.erp_servers.Cost.CostApportionmentService;
import com.erp.erp_servers.Cost.CostDetailService;
import com.erp.erp_servers.warehouse.IPlanInstoreProductService;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-06-14 4:03
 */
@Service
public class CostApportionmentAllService {
    private final CostApportionmentService costApportionmentService;

    private final IPlanInstoreProductService planInstoreProductService;

    private final CostDetailService costDetailService;

    public CostApportionmentAllService(CostApportionmentService costApportionmentService, IPlanInstoreProductService planInstoreProductService, CostDetailService costDetailService) {
        this.costApportionmentService = costApportionmentService;
        this.planInstoreProductService = planInstoreProductService;
        this.costDetailService = costDetailService;
    }

    /**
     * 保存入库费用分摊信息
     * @param info
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public R add(List<InsertPlanInStoreProductInfoReq> info) throws ParseException {
        int num=0;
        int numPlan=0;
        for (InsertPlanInStoreProductInfoReq req:info) {
            CostApportionmentEntity cEntity=new CostApportionmentEntity();
            cEntity.setCostCode(req.getCostCode());
            cEntity.setCostDetailId(req.getCostDetailId());
            cEntity.setBillno(req.getBillNo());
            cEntity.setCarryDetailNo(req.getCarryDetailNo());
            if (StringUtils.isNotEmpty(req.getTimeStr())){
                cEntity.setInstoreTime(DateUtils.convertStringToDate(req.getTimeStr(),DateUtils.DATE_PATTERN));
            }

            cEntity.setWarehouseId(req.getWarehouseId());
            cEntity.setWarehouseName(req.getWarehouseName());
            cEntity.setApportionmentAmount(req.getShareCostPrice());
            cEntity.setLotNo(req.getLotNo());
            cEntity.setProductCode(req.getProductCode());
            if (req.getNum()!=null){
                cEntity.setNum(req.getNum().intValue());
            }

            cEntity.setGrossWeight(req.getGrossWeight());
            cEntity.setNetWeight(req.getNetWeight());
            cEntity.setBulk(req.getVolume());
            cEntity.setPersonCode(req.getPersonNo());
            cEntity.setPersonName(req.getPerson());
            cEntity.setOperatorCode(req.getOperationCode());
            cEntity.setOperatorName(req.getOperation());
            cEntity.setAllocationtype(Integer.parseInt(req.getAllocationType()));
            //保存入库费用分摊信息
            num=num+costApportionmentService.insertCostApportionmentInfo(cEntity);

            //根据费用明细id获取信息，更改分摊字段
            CostDetailEntity costDetailEntity=costDetailService.findCostDetailById(req.getCostDetailId());
            costDetailEntity.setShare(1);
            costDetailService.updateByCostDetailEntity(costDetailEntity);


            //根据备货入库产品ID查询备货入库信息
            PlanInstoreProductEntity pProductEntity=planInstoreProductService.queryById(req.getPlanInStoreProductId());

            //获取旧的分摊金额
            BigDecimal shareCostPrice=new BigDecimal(0);
            if (pProductEntity.getShareCostPrice()!=null){
                shareCostPrice=pProductEntity.getShareCostPrice();
            }
            //获取原来的海运费
            BigDecimal shippingFeeBefore=new BigDecimal(0);
            if (pProductEntity.getShippingFee()!=null){
                shippingFeeBefore=pProductEntity.getShippingFee();
            }
            //获取原来的关税
            BigDecimal tariffBefore=new BigDecimal(0);
            if (pProductEntity.getTariff()!=null){
                tariffBefore=pProductEntity.getTariff();
            }
            //获取原来的美国仓储操作费
            BigDecimal storageOperationFeeBefore=new BigDecimal(0);
            if (pProductEntity.getStorageOperationFee()!=null){
                storageOperationFeeBefore=pProductEntity.getStorageOperationFee();
            }
            //获取原来的运输代理服务费
            BigDecimal transferAgencyFeeBefore=new BigDecimal(0);
            if(pProductEntity.getTransferAgencyFee()!=null){
                transferAgencyFeeBefore= pProductEntity.getTransferAgencyFee();
            }

            //费用类型
            //..(4,9,10,8) /*海运费,美国仓储操作费,运输代理服务费,关税*/
            switch (req.getFeeCode()){
                case "4"://海运费
                    pProductEntity.setShippingFee(shippingFeeBefore.add(req.getShareCostPrice()));
                    break;
                case "8"://关税
                    pProductEntity.setTariff(tariffBefore.add(req.getShareCostPrice()));
                    break;
                case "9"://美国仓储操作费
                    pProductEntity.setStorageOperationFee(storageOperationFeeBefore.add(req.getShareCostPrice()));
                    break;
                case "10"://运输代理服务费
                    pProductEntity.setTransferAgencyFee(transferAgencyFeeBefore.add(req.getShareCostPrice()));
                    break;
            }

            //获取计算之后的海运费
            BigDecimal shippingFeeAfter=new BigDecimal(0);
            if (pProductEntity.getShippingFee()!=null){
                shippingFeeAfter=pProductEntity.getShippingFee();
            }
            //获取计算之后的关税
            BigDecimal tariffAfter=new BigDecimal(0);
            if (pProductEntity.getTariff()!=null){
                tariffAfter=pProductEntity.getTariff();
            }

            //获取计算之后的美国仓储操作费
            BigDecimal storageOperationFeeAfter=new BigDecimal(0);
            if (pProductEntity.getStorageOperationFee()!=null){
                storageOperationFeeAfter=pProductEntity.getStorageOperationFee();
            }
            //获取计算之后的运输代理服务费
            BigDecimal transferAgencyFeeAfter=new BigDecimal(0);
            if(pProductEntity.getTransferAgencyFee()!=null){
                transferAgencyFeeAfter= pProductEntity.getTransferAgencyFee();
            }

            //加上新的分摊金额总计
            pProductEntity.setShareCostPrice(shareCostPrice.add(shippingFeeAfter).add(tariffAfter).add(storageOperationFeeAfter).add(transferAgencyFeeAfter));

            numPlan =numPlan+planInstoreProductService.update(pProductEntity);

        }

        if (num==info.size() && numPlan==info.size()){
            return R.build(ResultStatusEnum.SUCCESS);
        }
        return R.build(ResultStatusEnum.SYSTEM_ERROR);
    }

}
